#ifndef AJ_SESSION_THREADS_H
#define AJ_SESSION_THREADS_H

#include "aj_namespace.h"
#include <vector>
#include <unordered_map>
#include <stdint.h>
#include <memory>
#include <mutex>
#include "aj_tcpthread.h"
#include "aj_tcpsession.h"

NAMESPACE_NETAPI_BEGIN
class SessionThreads : public QObject
{
    Q_OBJECT
public:
    SessionThreads();
    ~SessionThreads();

    //启动线程池, 非线程安全
    bool Start(uint threadNum);
    //关闭, 非线程安全
    void Stop();
    //获取最小会话数线程
    TcpThread *PickMinThread();
    //获取会话数
    std::vector<uint> GetSessionSize()const;
    //添加会话
    void AddSession(std::shared_ptr<TcpSession> &session);

    private slots:
    //会话断开
    void SlotSessionDisConnected(void *id);

private:
    std::vector<TcpThread*> m_threadList;
    //互斥量
    std::mutex m_lock;
    //会话列表
    std::unordered_map<void*, std::shared_ptr<TcpSession>> m_sessionList;

    bool m_isRunning= false;
};

NAMESPACE_NETAPI_END
#endif // AJ_SESSION_THREADS_H
